package com.sanerzone.smscenter.modules.sms.web.util;

import com.sanerzone.smscenter.biz.entity.SmsSendVo;
import com.sanerzone.smscenter.modules.sms.entity.SmsDetail;
import com.sanerzone.smscenter.modules.sms.entity.SmsSend;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ControllerUtil {
    public static void removeComma(SmsSend smsSend){
        String res=smsSend.getSendStatus();
        //这里前端页面传来的参数出现逗号bug  暂时如此修复一下
        if(res!=null&&res.indexOf("," )!=-1){
            res=res.replace( ',',' ' );
            res=res.trim();
            smsSend.setSendStatus( res );
        }
    }
    public static SmsSendVo SmsSendToVo(SmsSend smsSend){
        SmsSendVo smsSendVo=new SmsSendVo();
        BeanUtils.copyProperties( smsSend,smsSendVo );

         return smsSendVo;
    }

    public static void filter(List<SmsSend> smsSends){
            Iterator<SmsSend> smsSendIterator= smsSends.iterator();
            while (smsSendIterator.hasNext()){
                SmsSend smsSend=  smsSendIterator.next();
                if ((smsSend.getSendStatus().startsWith( "F" )||smsSend.getSendStatus().startsWith( "T" ))&&smsSend.getShowLevel().equals( "2" )){
                    smsSendIterator.remove();
                }
            }
        }


    public static List<SmsDetail> smsSend2SmsDetail(List<SmsSend> smsSends){
        String statusV="待审核";
        String statusD="待处理";
        String statusF="发送失败";
        List<SmsDetail> smsDetails=new ArrayList<>();
        for (int i=0;i<smsSends.size();i++){
            SmsSend temp=smsSends.get( i );
            SmsDetail smsDetail=new SmsDetail();
            String sendStatus=temp.getSendStatus();
            if (sendStatus.startsWith( "V" )){
                smsDetail.setStatus( statusV );
                if ("V001".equals( sendStatus )){
                    smsDetail.setStatusReason( "签名不存在" );
                }
                if ("V002".equals( sendStatus )){
                    smsDetail.setStatusReason( "未找到签名" );
                }
                if ("V003".equals( sendStatus )){
                    smsDetail.setStatusReason( "敏感字" );
                }
                if ("V004".equals( sendStatus )){
                    smsDetail.setStatusReason( "无此短信模板" );
                }
                if ("V005".equals( sendStatus )){
                    smsDetail.setStatusReason( "路由模板失败" );
                }
                if ("V006".equals( sendStatus )){
                    smsDetail.setStatusReason( "网关匹配失败" );
                }
                if ("V007".equals( sendStatus )){
                    smsDetail.setStatusReason( "网关匹配失败" );
                }
                if ("V008".equals( sendStatus )){
                    smsDetail.setStatusReason( "网关匹配失败" );
                }
                if ("V009".equals( sendStatus )){
                    smsDetail.setStatusReason( "网关匹配失败" );
                }
                if ("V010".equals( sendStatus )){
                    smsDetail.setStatusReason( "号段匹配异常" );
                }
            }else if (sendStatus.startsWith( "D" )){
                smsDetail.setStatus( statusD );
                smsDetail.setStatusReason( "网关下发有误" );
            }else if (sendStatus.startsWith( "F" )){
                if ("F000".equals( sendStatus )){
                    smsDetail.setStatusReason( "客服置为失败" );
                }
                if ("F001".equals( sendStatus )){
                    smsDetail.setStatusReason( "敏感字" );
                }
                if ("F002".equals( sendStatus )){
                    smsDetail.setStatusReason( "黑名单" );
                }
                if ("F003".equals( sendStatus )){
                    smsDetail.setStatusReason( "非允许运营商" );
                }
                if ("F004".equals( sendStatus )){
                    smsDetail.setStatusReason( "非允许运营商" );
                }
                smsDetail.setStatus( statusF );
            }else if (sendStatus.startsWith( "X" )){
                smsDetail.setStatus( "状态未知" );
                smsDetail.setStatusReason( "网关调用延时等原因" );
            }
            else {
                //这里就是T的情况了  或者是S的情况


                String gatewayStatus=temp.getGatewayStatus();
                if (gatewayStatus.startsWith( "F" )){
                        smsDetail.setStatus( "下发网关失败" );
                        smsDetail.setStatusReason( "下发异常" );
                }else if (gatewayStatus.startsWith( "P" )){
                        smsDetail.setStatus( "状态未知" );
                        smsDetail.setStatusReason( "等待回调确认" );
                }else{
                    smsDetail.setStatus( "下发成功" );
                    smsDetail.setStatusReason( "√" );
                }
            }
            smsDetail.setPhone( temp.getPhone() );
            smsDetail.setSmsSize( temp.getSmsSize() );
            smsDetail.setStatusTime( temp.getStatusChangeTime() );
            smsDetail.setSubmitTime( temp.getReceiveTime() );
            smsDetails.add( smsDetail );
        }
        return smsDetails;
    }
}
